package com.qd.p2p.web.action.user;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UserPwdQuestionService;
import com.qdlc.p2p.biz.service.user.UserMobilePhoneService;
import com.qdlc.p2p.biz.util.UserLoginUtil;
import com.qdlc.p2p.biz.util.ValidCodeSender;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserPwdQuestion;
import com.qdlc.p2p.dal.model.UserModel;

/**
 * 绑定用户手机号码
 * @author linya
 */
public class UserMobilePhoneAction extends BaseAction<User> implements ModelDriven<User> {

	@Resource
	UserPwdQuestionService userPwdQuestionService;
	
	@Resource
	UserMobilePhoneService userMobilePhoneService;
	
	
    /**
     * 修改绑定手机-获取验证码
     * @return
     * @throws Exception
     */
    @Action("/member/security/modifyPhoneCode")
    @Deprecated
    public void modifyPhoneCode() throws Exception {
        User user = getSessionUser();
        ReturnResult result = ValidCodeSender.newInstance().sendCodeByEmail(user, model.getEmail(),
                NoticeTplNid.NOTICE_MODIFY_PHONE);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }

    /**
     * 修改绑定手机-校验验证码
     *
     * @return
     * @throws Exception
     */
    @Action("/member/security/checkModifyPhoneCode")
    @Deprecated
    public void checkModifyPhoneCode() throws Exception {
        User user = getSessionUser();
        String code = paramString("code");
        ReturnResult result = ValidCodeSender.checkValidCode(user, code,
                NoticeTplNid.NOTICE_MODIFY_PHONE);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }

    /**
     * 修改绑定手机 校验密码和密保
     *
     * @return
     */
    @Action("/member/security/doModifyPhone")
    @Deprecated
    public void doModifyPhone() throws Exception {
        User user = UserLoginUtil.getLoginUser();
        UserModel model = UserModel.instance(user);
        UserPwdQuestion question = userPwdQuestionService.find(model.getQuestion(), user.getUserId());
        model.validUserQuestion(user, question);
        printWebSuccess();
    }

    /**
     * [绑定手机]获取手机验证码
     */
    @Action("/member/security/bindPhoneCode")
    public void bindPhoneCode() throws Exception {
        User user = getSessionUser();
        ReturnResult result = ValidCodeSender.newInstance().sendCodeByPhone(user,
                model.getMobilePhone(), NoticeTplNid.NOTICE_BIND_PHONE);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }

    /**
     * [绑定手机] 绑定手机号码
     */
    @Action("/member/security/doBindPhone")
    public void doBindPhone() throws Exception {
        User user = getSessionUser();
        String code = paramString("code");
        String mobilePhone = paramString("mobilePhone");
        ReturnResult result = userMobilePhoneService.bindMobilePhone(user, code, mobilePhone);
        printWebResult(result.getErrorInfo(), result.isSuccess());
    }
    
}
